﻿using Handy_WPF.MVVM;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace Handy_DNS.ViewModel
{
    public class PageBaseViewModel<T> : NotificationProperty where T : class
    {
        /// <summary>
        /// 数据列表
        /// </summary>
        private IList<T> dataList;

        /// <summary>
        ///     数据列表
        /// </summary>
        public IList<T> DataList
        {
            get => dataList;
            set
            {
                dataList = value;
                this.IsRunning = false;
                RaisePropertyChanged("DataList");
            }
        }

        #region UI
        /// <summary>
        /// 加载显示样式
        /// </summary>
        private Visibility loadding = Visibility.Visible;
        public Visibility Loadding
        {
            get { return loadding; }
            set { loadding = value; RaisePropertyChanged("Loadding"); }
        }

        /// <summary>
        /// 是否加载中
        /// </summary>
        private bool isRunning = true;
        public bool IsRunning
        {
            get { return isRunning; }
            set { isRunning = value; if (value) { Loadding = Visibility.Visible; } else { Loadding = Visibility.Hidden; } RaisePropertyChanged("Loadding"); }
        }
        #endregion

        #region 分页
        /// <summary>
        /// 页码
        /// </summary>
        private int pageIndex = 1;
        public int PageIndex
        {
            get => pageIndex;
            set
            {
                pageIndex = value;
                RaisePropertyChanged("PageIndex");
            }
        }

        /// <summary>
        /// 页大小
        /// </summary>
        private int pageSize = 10;
        public int PageSize
        {
            get { return pageSize; }
            set
            {
                pageSize = value;
                RaisePropertyChanged("PageSize");
            }
        }

        /// <summary>
        /// 列表总数
        /// </summary>
        private int totalCount = 0;
        public int TotalCount
        {
            get { return totalCount; }
            set
            {
                totalCount = value;
                if (totalCount % PageSize == 0) PageMaxCount = totalCount / PageSize;
                else PageMaxCount = totalCount / PageSize + 1;

                if (PageIndex > PageMaxCount) PageIndex = PageMaxCount;

                RaisePropertyChanged("TotalCount");
            }
        }

        /// <summary>
        /// 最大页数
        /// </summary>
        private int pageMaxCount = 1;
        public int PageMaxCount
        {
            get { return pageMaxCount; }
            set
            {
                pageMaxCount = value;
                RaisePropertyChanged("PageMaxCount");
            }
        }
        #endregion
    }
}
